package com.houfukude.Activity;

import java.util.ArrayList;

import org.apache.commons.net.ftp.FTPFile;

import com.houfukude.NavigationDrawerFragment;
import com.houfukude.PlaceholderFragment;
import com.houfukude.API.NavigationDrawerCallbacks;
import com.houfukude.Utils.ActionBar_Utils;
import com.houfukude.Utils.FTP_Client;
import com.houfukude.xtestmgr.R;

import Utils.V;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.FragmentManager;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import android.support.v4.widget.DrawerLayout;

public class MainActivity extends ActionBarActivity implements
		NavigationDrawerCallbacks {
	private String DEBUG_TAG = "MainActivity";
	/**
	 * Fragment managing the behaviors, interactions and presentation of the
	 * navigation drawer.
	 */
	private NavigationDrawerFragment mNavigationDrawerFragment;

	/**
	 * Used to store the last screen title. For use in
	 * {@link ActionBar_Utils#restoreActionBar(CharSequence, android.support.v7.app.ActionBar)
	 * restoreActionBar}.
	 */
	private CharSequence mTitle;
	// 用于保存当前页面的参数
	private int number;

	// 用于保存第一界面相关参数
	public LinearLayout ftp_layout;
	public FTP_Client ftp_Client;
	public ArrayList<FTPFile> list;
	public Button[] buttons;

	// 用于MainActivity各种子线程通知主线程刷新界面用
	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			// FTP_Client初始化
			case V.what.FTP_Initalize:
				buttons = new Button[list.size()];
				for (int i = 0; i < list.size(); i++) {
					buttons[i] = new Button(MainActivity.this);
					buttons[i].setText("文件：" + list.get(i).getName() + " 大小："
							+ list.get(i).getSize());
					buttons[i].setId(i);
					buttons[i].setTag(V.tag.FTP_Button);
					buttons[i].setOnClickListener(btnListener);
					ftp_layout.addView(buttons[i]);
				}
				ftp_layout.invalidate();
				break;
			// FTP_Client 下载提醒
			case V.what.FTP_Download_Complete:
				Toast.makeText(getApplicationContext(),
						"下载:" + msg.obj.toString() + "  !", Toast.LENGTH_LONG)
						.show();
				break;

			default:
				break;
			}

		}
	};
	/**
	 * 用于MainActivity中各种点击事件的处理 <br>
	 * 使用{@link View#getTag() getTag()}来判断具体view
	 */
	private OnClickListener btnListener = new OnClickListener() {
		@Override
		public void onClick(final View view) {
			switch (view.getTag().toString()) {
			case V.tag.FTP_Button:
				new Thread(new Runnable() {
					@Override
					public void run() {
						Message msg = new Message();
						if (ftp_Client.get(view.getId())) {

							msg.obj = ((Button) view).getText() + "完成";
						} else {
							msg.obj = ((Button) view).getText() + "失败";
						}
						msg.what = V.what.FTP_Download_Complete;
						handler.sendMessage(msg);
					}
				}).start();

				break;

			default:
				break;
			}

		}
	};

	public void method_快速按钮执行(View view_传入视图) {
		
		switch (view_传入视图.getTag().toString()) {
		case "数据交换":
			Toast.makeText(this, "数据交换", Toast.LENGTH_SHORT)
					.show();
			Intent intent_数据交换 = new Intent("com.houfukude.Activity.FtpActivity");
			startActivityForResult(intent_数据交换, 0);
			break;
		case "记事本":
			Toast.makeText(this, "记事本", Toast.LENGTH_SHORT)
					.show();
			Intent intent_记事本 = new Intent(
					"com.houfukude.Activity.NoteListActivity");
			startActivityForResult(intent_记事本, 0);
			break;
		case "计算器":
			Toast.makeText(this, "计算器", Toast.LENGTH_SHORT)
					.show();
			Intent intent_计算器 = new Intent(
					"com.houfukude.Activity.CalculateActivity");
			startActivityForResult(intent_计算器, 0);
			break;
		case "测试":
			Toast.makeText(this, "测试", Toast.LENGTH_SHORT)
					.show();
			Intent intent_测试 = new Intent(
					"com.houfukude.Activity.testActivity");
			startActivityForResult(intent_测试, 0);
			break;
		
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
				.findFragmentById(R.id.navigation_drawer);
		mTitle = getTitle();

		// Set up the drawer.
		mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
				(DrawerLayout) findViewById(R.id.drawer_layout));

	}

	@Override
	public void onNavigationDrawerItemSelected(int position) {
		// update the main content by replacing fragments
		FragmentManager fragmentManager = getSupportFragmentManager();
		fragmentManager
				.beginTransaction()
				.replace(R.id.container,
						PlaceholderFragment.newInstance(position + 1)).commit();
	}

	@SuppressWarnings("deprecation")
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		number = mNavigationDrawerFragment.getCurrentSelectedPosition() + 1;
		// Log.i(DEBUG_TAG , "ID:"+ID );
		if (!mNavigationDrawerFragment.isDrawerOpen()) {
			// Only show items in the action bar relevant to this screen
			// if the drawer is not showing. Otherwise, let the drawer
			// decide what to show in the action bar.
			switch (number) {
			case 1:
				getMenuInflater().inflate(R.menu.ftp_menu, menu);
				mTitle = getString(R.string.title_section1);
				ActionBar_Utils.restoreActionBar(mTitle, getSupportActionBar());
				return true;
			case 2:
				getMenuInflater().inflate(R.menu.note_list, menu);
				mTitle = getString(R.string.title_section2);

				ActionBar_Utils.restoreActionBar(this, getSupportActionBar());
				return true;
			case 3:
				getMenuInflater().inflate(R.menu.main, menu);
				mTitle = getString(R.string.title_section3);
				ActionBar_Utils.restoreActionBar(mTitle, getSupportActionBar());
				return true;
			default:
				break;
			}

		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		Log.d(DEBUG_TAG, "ItemSelected item.getItemId():" + item.getItemId());
		switch (item.getItemId()) {
		/**
		 * ftp_menu.xml
		 */
		case R.id.action_server:
			ActionBar_Utils.ServerItemSelected(item, this);
			break;
		case R.id.action_openap:
			ActionBar_Utils.OpenapItemSelected(item, this);
			break;
		case R.id.action_openwifi:
			ActionBar_Utils.OpenwifiItemSelected(item, this);
			break;
		/**
		 * note_list.xml
		 */
		case R.id.action_add_note:
			ActionBar_Utils.AddNoteItemSelected(item, this);
			break;
		case R.id.action_search:
			ActionBar_Utils.SearchNoteItemSelected(item, this);
			break;
		case R.id.action_filter:
			ActionBar_Utils.NoteFilterItemSelected(item, this);
			break;
		/**
		 * main.xml&&all
		 */
		case R.id.action_settings:
			Toast.makeText(this, "settings for " + getTitle(),
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.action_refresh:

			ActionBar_Utils.RefreshItemSelected(item, this);
			break;
		case R.id.action_about:
			Toast.makeText(this, getString(R.string.ftp_thx), Toast.LENGTH_LONG)
					.show();
			break;
		case R.id.action_test:
			ActionBar_Utils.TestItemSelected(item, this);
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// super.onActivityResult(arg0, arg1, arg2);
		Log.i(DEBUG_TAG, "Main requestCode : " + requestCode + " resultCode :"
				+ resultCode);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case V.id.ADD_NOTE:
				Toast.makeText(this, "新记事已添加完成！", Toast.LENGTH_SHORT).show();
				break;
			// case V.id.FILTER_NOTE:
			// Toast.makeText(this, "筛选中...", Toast.LENGTH_SHORT)
			// .show();
			// break;
			default:
				break;
			}

		}

	}

}
